﻿using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
using GrayParrot.Web.Controls;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.Net;

namespace $rootnamespace$
{
	public class $safeitemrootname$ : GridCrudController</* PUT HERE THE TYPE OF CRUD OBJECT */, DTParameters>
	{
        public override System.Net.Http.HttpResponseMessage Load(DTParameters data)
        {
            System.Net.Http.HttpResponseMessage retValue = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            using (YOUR_FACADE m_dc = new YOUR_FACADE())
            {
                m_dc.Initialize();

                IQueryable<YOUR_DATA_OBJECT> m_objs = m_dc.YOUR_DATA_GET_METHOD();

                YOUR_RESULT_SET m_rs = new YOUR_RESULT_SET(data, m_objs);
                DTResult<YOUR_DATA_OBJECT> result = m_rs.GetResult();

                retValue = Request.CreateResponse(HttpStatusCode.OK, result);
            }

            return retValue;
        }

        public override System.Net.Http.HttpResponseMessage Add(/* PUT HERE THE TYPE OF CRUD OBJECT */ data)
        {
            System.Net.Http.HttpResponseMessage retValue = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            dynamic json = data;

            YOUR_DATA_OBJECT obj = new YOUR_DATA_OBJECT();

            using (YOUR_FACADE m_dc = new YOUR_FACADE())
            {
                m_dc.Initialize();

                bool result = m_dc.YOUR_DATA_SAVE_METHOD(obj);

                if (result)
                {
                    retValue = Request.CreateResponseMessage(YOUR_SUCCESS_MESSAGE, ResponseMessageTypes.Success);
                }
                else
                {
                    retValue = Request.CreateResponseMessage(YOUR_FAILURE_MESSAGE, ResponseMessageTypes.Error);
                }
            }

            return retValue;
        }

        public override System.Net.Http.HttpResponseMessage Edit(/* PUT HERE THE TYPE OF CRUD OBJECT */ data)
        {
            System.Net.Http.HttpResponseMessage retValue = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            
            dynamic json = data;

            YOUR_DATA_OBJECT obj = new YOUR_DATA_OBJECT();

            using (YOUR_FACADE m_dc = new YOUR_FACADE())
            {
                m_dc.Initialize();

                bool result = m_dc.YOUR_DATA_EDIT_METHOD(obj);

                if (result)
                {
                    retValue = Request.CreateResponseMessage(YOUR_SUCCESS_MESSAGE, ResponseMessageTypes.Success);
                }
                else
                {
                    retValue = Request.CreateResponseMessage(YOUR_FAILURE_MESSAGE, ResponseMessageTypes.Error);
                }
            }

            return retValue;
        }

        public override System.Net.Http.HttpResponseMessage Delete(JObject data)
        {
            System.Net.Http.HttpResponseMessage retValue = new HttpResponseMessage(HttpStatusCode.BadRequest);

            dynamic json = data;
            Guid idRecord = json.id;

            using (AssociazioniFacade m_dc = new AssociazioniFacade())
            {
                m_dc.Initialize();

                bool result = m_dc.DeleteAssociazioneGiocatori(idRecord);

                if (result)
                {
                    retValue = Request.CreateResponse(HttpStatusCode.OK, "Cancellazione associazione effettuata !");
                }
                else
                {
                    retValue = Request.CreateResponse(HttpStatusCode.OK, "Errore nella cancellazione della associazione");
                }
            }

            return retValue;
        }

        public override HttpResponseMessage Delete(int id)
        {
            throw new NotImplementedException();
        }
	}
}
